package m3;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //连接服务器
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        //5672端口，客户端应用收发消息的端口
        //15672端口，管理控制台界面的访问端口
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection();
        Channel c = con.createChannel();

        //创建fanout类型的交换机 ：logs
        c.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);
//        c.exchangeDeclare("logs", "fanout");

        //向交换机发送消息
        while (true) {
            System.out.println("请输入消息：");
            String s = new Scanner(System.in).nextLine();
            //第二个参数对于fanout交换机无效，因为fanout是群发
            c.basicPublish("logs", "", null, s.getBytes(StandardCharsets.UTF_8));
        }
    }
}
